home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / SEARCH / RUBICON / PREVINST.PAS < prev    next >
Pascal/Delphi Source File  |  1996-10-21  |  2KB  |  106 lines

  1. {$B-,G+,X+}    {* Boolean evaluation, Generate 286 code, eXtended syntax *}
  2.  unit PrevInst;
  3.  
  4.  interface
  5.  
  6.  uses
  7.   {$IFDEF WIN32}
  8.   Windows,
  9.   {$ELSE}
  10.   WinTypes, WinProcs,
  11.   {$ENDIF}
  12.    SysUtils;
  13.  
  14. {$IFDEF WIN32}
  15. function DoIExist(WndTitle : String) : Boolean;
  16.  
  17. implementation
  18.  
  19. function DoIExist(WndTitle : String) : Boolean;
  20. var
  21.   hSem      : THandle;
  22.   hWndMe,
  23.   hWndPrev : HWnd;
  24.   semNm,
  25.   wTtl      : Array[0..256] of Char;
  26. begin
  27.  
  28.   Result := False;
  29.  
  30.   //Initialize arrays
  31.   StrPCopy(semNm, 'SemaphoreName');
  32.   StrPCopy(wTtl, WndTitle);
  33.  
  34.   //Create a Semaphore in memory - If this is the first instance, then
  35.   //it should be 0.
  36.   hSem := CreateSemaphore(nil, 0, 1, semNm);
  37.  
  38.   //Now, check to see if the semaphore exists
  39.   if ((hSem <> 0) AND (GetLastError() = ERROR_ALREADY_EXISTS)) then begin
  40.     CloseHandle(hSem);
  41.  
  42.     //We'll first get the currently executing window's handle then change its title
  43.     //so we can look for the other instance
  44.     hWndMe := FindWindow(nil, wTtl);
  45.     SetWindowText(hWndMe, 'zzzzzzz');
  46.  
  47.     //What we want to do now is search for the other instance of this window
  48.     //then bring it to the top of the Z-order stack.
  49.     hWndMe := FindWindow(nil, wTtl);
  50.     if (hWndMe <> 0) then begin
  51.       if IsIconic(hWndMe) then
  52.     ShowWindow(hWndMe, SW_SHOWNORMAL)
  53.       else
  54.     SetForegroundWindow(hWndMe);
  55.     end;
  56.  
  57.     Result := True;
  58.  
  59.     //Could put the Halt here, instead of in the FormCreate method,
  60.     //unless you want to do some extra processing.
  61.  
  62.     //Halt;
  63.   end;
  64. end;
  65.  
  66. {$ELSE}
  67.  
  68.  type
  69.   PHWND = ^HWND;
  70.   function  EnumFunc(Wnd:HWND; TargetWindow:PHWND): bool; export;
  71.   procedure GotoPreviousInstance;
  72.  
  73.  implementation
  74.  
  75.   function EnumFunc(Wnd:HWND; TargetWindow:PHWND): bool;
  76.   var
  77.     ClassName : array[0..30] of char;
  78.   begin
  79.     Result := true;
  80.     if GetWindowWord(Wnd,GWW_HINSTANCE) = hPrevInst then
  81.        begin
  82.        GetClassName(Wnd,ClassName,30);
  83.        if StrIComp(ClassName,'TApplication') = 0 then
  84.       begin
  85.        TargetWindow^ := Wnd;
  86.        Result := false;
  87.       end;
  88.        end;
  89.   end;
  90.  
  91.   procedure GotoPreviousInstance;
  92.   var
  93.     PrevInstWnd : HWND;
  94.   begin
  95.     PrevInstWnd := 0;
  96.     EnumWindows(@EnumFunc,longint(@PrevInstWnd));
  97.     if PrevInstWnd <> 0 then
  98.        if IsIconic(PrevInstWnd) then
  99.       ShowWindow(PrevInstWnd,SW_RESTORE)
  100.        else
  101.       BringWindowToTop(PrevInstWnd);
  102.   end;
  103. {$ENDIF}
  104.  
  105.   end.
  106.